package com.library.base;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author : 凉小天
 * @date : 2021/6/29 14:30
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class R<T> implements Serializable {
    public static final long serialVersionUID = 1L;

    public static final long SUCCESS = 200;  //成功
    public static final  String SUCCESS_MESSAGE = "执行成功";//  执行成的消息

    public static final long ERROR = 500;  //失败
    public static final  String ERROR_MESSAGE = "执行失败";

    private long code; //状态码
    private T data; //数据
    private String  msg;  //信息


    public static <T> R<T> result(T data){
        if (data ==null){
            return fail(null);
        }
        if (data instanceof Boolean){
            return (Boolean)data ? ok(null) : fail(null);
        }else {
            return ok(data);
        }

    }


    public static <T> R<T> result (Long code,String msg){
        return new R<T>(code,null,msg);
    }

    public static <T> R<T> result (T data ,String msg){
        R<T> r = result(data);
        r.setMsg(msg);
        return r;
    }


    //执行成功
    public static <T> R<T> ok(T data){
        return new R<T>(SUCCESS,data,SUCCESS_MESSAGE);
    }

    //执行失败
    public static <T> R<T>fail(T data){
        return new R<T>(ERROR,data,ERROR_MESSAGE);
    }



}
